package com.cino.order.service.impl;

import com.cino.basic.util.LoginContext;
import com.cino.basic.util.PageList;
import com.cino.order.domain.OrderPetAcquisition;
import com.cino.order.dto.OrderDto;
import com.cino.order.mapper.OrderPetAcquisitionMapper;
import com.cino.order.query.OrderPetAcquisitionQuery;
import com.cino.order.service.IOrderPetAcquisitionService;
import com.cino.basic.base.service.impl.BaseServiceImpl;
import com.cino.org.domain.Employee;
import com.cino.org.domain.Shop;
import com.cino.org.mapper.EmployeeMapper;
import com.cino.org.mapper.ShopMapper;
import com.cino.pet.domain.Pet;
import com.cino.pet.domain.SearchMasterMsg;
import com.cino.pet.mapper.PetMapper;
import com.cino.pet.mapper.SearchMasterMsgMapper;
import com.cino.users.domain.Logininfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;

/**
 * 业务实现类：
 */
@Service
public class OrderPetAcquisitionServiceImpl extends BaseServiceImpl<OrderPetAcquisition> implements IOrderPetAcquisitionService {
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private SearchMasterMsgMapper searchMasterMsgMapper;
    @Autowired
    private OrderPetAcquisitionMapper orderPetAcquisitionMapper;
    @Autowired
    private PetMapper petMapper;

    @Override
    public PageList<OrderPetAcquisition> pageQuery(OrderPetAcquisitionQuery query, HttpServletRequest request) {
        Logininfo logininfo = LoginContext.getLogininfo(request);
        Employee employee = employeeMapper.loadByLogininfoId(logininfo.getId());
        if (employee.getShop_id() != null){//店铺管理员=店长 or 店员
            Shop shop = shopMapper.loadById(employee.getShop_id());
            if (shop.getAdminId() != employee.getId()) {//店员
                query.setEmpId(employee.getId());
            }
            query.setShopId(employee.getShop_id());
        }
        return super.pageQuery(query);
    }

    @Override
    public void cancel(Long id) {
        //1.将寻主消息放入寻主池:state=3，shopId=null
        OrderPetAcquisition order = orderPetAcquisitionMapper.loadById(id);
        searchMasterMsgMapper.reject(order.getSearchMasterMsgId());
        //2.修改订单的状态为-1
        order.setState(-1);
        orderPetAcquisitionMapper.update(order);

    }

    @Override
    public void confirm(OrderDto orderDto) {
//订单确认:处理人去用户哪里把宠物接回来 。 平台就会多一个宠物，需要录入
        Long orderId =orderDto.getId();
        BigDecimal money=orderDto.getMoney();//最终成交价
        Integer payType =orderDto.getPayType();//支付方式
//1.生成宠物对象
        OrderPetAcquisition order=orderPetAcquisitionMapper.loadById(orderId);
        SearchMasterMsg msg=searchMasterMsgMapper.loadById(order.getSearchMasterMsgId());
        Pet pet =createPet(msg,money);
        petMapper.save(pet);
//2.修改订单状态1，并且要修改金额，最终成交价
        order.setState(1);
        order.setPrice(money);
        order.setPetId(pet.getId());
        orderPetAcquisitionMapper.update(order);
//3.修改寻主消息的状态:state=2
        msg.setState(2);//寻主消息完成
        searchMasterMsgMapper.update(msg);
    }

    private Pet createPet(SearchMasterMsg msg, BigDecimal money) {
        Pet pet = new Pet();
        pet.setName(msg.getName());
        pet.setCostprice(money);
        pet.setSaleprice(money);
        pet.setTypeId(msg.getPetType());
        pet.setResources(msg.getResources());
        pet.setShopId(msg.getShopId());
      //pet.setUserId(msq.qetUserId()); //这个是领养人的id
        pet.setSearchMasterMsgId(msg.getId());
        return pet;
    }
}